
from tkinter import *


root = Tk()

menubar = Menu(root)

def callback():
    print("hello ")

def saveFn():
    print("saved ")


# tearoff 撕开菜单
filemenu = Menu(menubar,tearoff=False)


filemenu.add_command(
    label="hello",
    command = callback)

filemenu.add_command( 
    label="save",
    command = saveFn)

filemenu.add_separator()

filemenu.add_command( 
    label="close",
    command = root.quit)

menubar.add_cascade(
    label="first",
    menu=filemenu)


editmenu = Menu(menubar)

editmenu.add_command(
    label="opposite",
    command = callback)

editmenu.add_command( 
    label="save",
    command = saveFn)

menubar.add_cascade(
    label="edit",
    menu=editmenu)


# 弹出菜单

alertMenu  =Menu(root)

def addd():
    print("add")

def decrease():
    print("decrease")

mc1= IntVar()
mc2= IntVar()
mc3= IntVar()

alertMenu.add_command(
    label="decrease",
    command=decrease)
alertMenu.add_checkbutton(
    label="apple",
    variable=mc1,
    command=addd)
alertMenu.add_checkbutton(
    label="banana",
    variable=mc2,
    command=addd)
alertMenu.add_checkbutton(
    label="peach",
    variable=mc3,
    command=addd)

mr = IntVar()

alertMenu.add_radiobutton(
    label="radio1",
    variable=mr,
    value=1,
    command=addd)

alertMenu.add_radiobutton(
    label="radio2",
    variable=mr,
    value=2,
    command=addd)


frame = Frame(root,width=400,height=200)

frame.pack()

def popup(event):
    alertMenu.post(event.x_root,event.y_root)

frame.bind("<Button-3>",popup)



# MenuButton

mb= Menubutton(root,
    text="我是Menubutton",
    relief=RAISED)

mb.pack()

menubar2 = Menu(mb)

menubar2.add_command(
    label="1",
    command=addd)
menubar2.add_command(
    label="2",
    command=addd)

mb.config(menu=menubar2)



# Option Menu

optons = [
"one","two"
]

variable = StringVar()

variable.set("one")

# * 表示 解包 和 打包
opmenu = OptionMenu(root,variable,*optons)


opmenu.pack()











root.config(menu=menubar)

mainloop()
